const { createProxyMiddleware } = require("http-proxy-middleware"),
  bodyParser = require("body-parser"),
  multipart = require("connect-multiparty"),
  multipartMiddleware = multipart(),
  OSS = require("ali-oss"),
  path = require("path"),
  client = new OSS({
    region: "oss-cn-shanghai",
    accessKeyId: "LTAI5t8bDur6gYtCJvgFfVKb",
    accessKeySecret: "sAPa8PyL7VK8ltJedAbSJBtJOH5EIF",
    bucket: "2007a-1",
  });
module.exports = app => {
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(
    "/api",
    createProxyMiddleware({
      target: "http://localhost:7001",
      changeOrigin: true,
      pathRewrite: { "^/api": "" },
    })
  );
  app.post("/upload/oss", multipartMiddleware, async (req, res) => {
    req.files.chunk.path;
    req.files.chunk.name;
    const result = await client.put(
      req.files.chunk.name,
      path.normalize(req.files.chunk.path)
    );
    res.send({
      code: 200,
      message: "上传成功",
      data: result,
    });
  });
};
